package com.example.webviewcrypt;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.crypt.CryptToolFactory;
import com.example.crypt.CryptUtils;
import com.example.crypt.ICrypt;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@SuppressLint("SetJavaScriptEnabled")
public class TestActivity3 extends Activity {
    public static String SCHEME_PROVIDER = "content://org.example.wv3";
    public static String APP_FILE_ROOT_IN_SD = Environment.getExternalStorageDirectory() + "/webview_test";
    public static String BASE_URL = SCHEME_PROVIDER + APP_FILE_ROOT_IN_SD + "/";

    private WebView webview;
    private Context mCtx;
    boolean isCryptFileGen = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_activity3);
        mCtx = this;

        //
        initWebview();
        init();
        LogMgr.i("APP_FILE_ROOT_IN_SD =" + APP_FILE_ROOT_IN_SD);
        LogMgr.i("BASE_URL =" + BASE_URL);
    }

    private void initWebview() {
        webview = (WebView) findViewById(R.id.wv_web1);
        // allows js
        webview.getSettings().setJavaScriptEnabled(true);
        // allows js alert windows
        webview.setWebChromeClient(new WebChromeClient());
        webview.setWebViewClient(new WebViewClient());// 设置WebView客户端对象
    }

    private void init() {
        final String fileName = "test/shake2.html";// "fund.html";//"shake.html";//
        final Button btnLoadCrypt = (Button) findViewById(R.id.btn_webview_load);
        final TextView tvFolderDir = (TextView) findViewById(R.id.tv_folder_dir);
        // 将asset目录下的文件，加密拷贝到sd卡上,备用
        findViewById(R.id.btn_encrypt).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // new AssetTool().copyAsset(mCtx, APP_FILE_ROOT_IN_SD);
                new AssetTool().copyAsset(mCtx,"test/shake2.html",APP_FILE_ROOT_IN_SD);//拷贝test 目录

                isCryptFileGen = true;
                tvFolderDir.setText("加密文件\n  sd/webview_test/"+fileName);
            }
        });


        // webview 加载明文文件 asset目录下，作为对比测试
        findViewById(R.id.btn_webview_load_non_encrypt).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    InputStream in = getAssets().open(fileName);
                    String data2 = CryptUtils.stream2String(in, "utf-8");
                    webview.loadDataWithBaseURL("file:///android_asset/", data2, "text/html", "utf-8", null);
                } catch(IOException e1) {
                    e1.printStackTrace();
                }
            }
        });



        // webview加载 加密文件 sd卡中
        btnLoadCrypt.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isCryptFileGen) {
                    Toast.makeText(mCtx, "请先生成加密文件", Toast.LENGTH_SHORT).show();
                    return;
                }
                ICrypt tool = CryptToolFactory.getInstance();
                try {
                    final String encryptFilePath = APP_FILE_ROOT_IN_SD + "/" + fileName;
                    InputStream fileStream = new FileInputStream(encryptFilePath);
                    InputStream decryptStream = tool.decryptStream(fileStream);
                    String data = CryptUtils.stream2String(decryptStream, "utf-8");
                    webview.loadDataWithBaseURL(BASE_URL, data, "text/html", "utf-8", null);
                } catch(IOException e) {
                    System.out.println(e.toString());
                    e.printStackTrace();
                }
            }
        });

        // webview加载 加密文件 sd卡中
        findViewById(R.id.btn_webview_load_direct).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isCryptFileGen) {
                    Toast.makeText(mCtx, "请先生成加密文件", Toast.LENGTH_SHORT).show();
                    return;
                }
                try {
                    final String encryptFilePath = APP_FILE_ROOT_IN_SD + "/" + fileName;
                    InputStream fileStream = new FileInputStream(encryptFilePath);
                    String data = CryptUtils.stream2String(fileStream, "utf-8");
                    webview.loadDataWithBaseURL(BASE_URL, data, "text/html", "utf-8", null);
                } catch(IOException e) {
                    System.out.println(e.toString());
                    e.printStackTrace();
                }
            }
        });
    }
}
